####################################################################
###R Code for Poll trends--Media trust in the Americas, 2008-2023###
####################################################################

library(tidyverse)
library(dplyr)
library(janitor)
library(ggplot2)
library(haven)
library(scales)
library(psych)

###DATA ASSEMBLY & CLEANING ###
###ACCESS DATA: https://www.vanderbilt.edu/lapop/about-americasbarometer.php ###
americasbar = read_dta("~/Grand_Merge_2004-2023_LAPOP_AmericasBarometer_v1.0_w.dta")
write.csv(americasbar, file = "AmericasBarometer_full.csv")
americasbar<-read.csv("AmericasBarometer_full.csv")

##removing 2004 and 2006 from the dataset (no Canada, US included prior to 2006) ##
americasbar_r<-filter(americasbar,!wave %in% c(2004, 2006, 2014, 2021))

##expanding analyses to include 20 countries ##
americasbar_r2.2<-filter(americasbar_r,!pais %in% c(16,
                                                    22, 24, 25, 26, 27, 28, 30))

##SURVEY WAVE AND PAIS AS FACTORS (NOT NUMERIC SCALE VALUES), RECODE AS COUNTRY NAMES ##
americasbar_r2.2$wave<-as.factor(americasbar_r2.2$wave)
americasbar_r2.2$pais<-as.factor(americasbar_r2.2$pais)

americasbar_r2.2$pais<-dplyr::recode(americasbar_r2.2$pais, "1" = "Mexico", "2" = "Guatemala", "3" = "El Salvador", "4" = "Honduras", 
                              "5" = "Nicaragua", "6" = "Costa Rica", "7" = "Panama" , "8" = "Colombia", "9" = "Ecuador",
                              "10" = "Bolivia", "11" = "Peru", "12" = "Paraguay", "13" = "Chile", "14" = "Uruguay" ,
                              "15" = "Brazil" , "17" = "Argentina", "21" = "Dominican Republic", "23" = "Jamaica", 
                              "40" = "United States", "41" = "Canada")

## FIGURE 1: Percentage media trust over time by country ## 
### creating a binary variable for 5,6,7 on media trust scale ###
americasbar_r2.2$trustmedia<-car::recode(americasbar_r2.2$b37,"1:4=0;5:7=1")

##FOR FIGURE 1 ##
americasbar2.3<-americasbar_r2.2 %>%
  drop_na(trustmedia) %>%
  group_by(pais, wave) %>%
  dplyr::count(trustmedia) %>%
  dplyr::mutate(perc = n/sum(n) * 100)
view(americasbar2.3)

figure1<-americasbar2.3 %>%
  filter(trustmedia != "0") %>%
  ggplot(aes(wave, perc, colour=pais)) +
  geom_point(shape = 15, size = 2) +
  geom_smooth() +
  geom_line(aes(group=pais)) + 
  facet_wrap(~factor(pais, c("Argentina","Bolivia","Brazil", "Canada",
                             "Chile",
                             "Colombia", "Costa Rica","Dominican Republic",
                             "Ecuador", "El Salvador", "Guatemala",
                             "Honduras","Jamaica",
                             "Mexico","Nicaragua", "Panama","Paraguay",
                             "Peru","United States","Uruguay"))) +
  theme_bw() +
  geom_hline(yintercept=0, linetype="dashed", color = "grey") +
  labs(x = "Year", y = "Percentage", colour = "Country")
show(figure1)

figure1 + coord_cartesian(xlim =c(1,6), ylim = c(0,100)) + 
  theme(text = element_text(family = "serif")) +
  theme(legend.position="none") +
  scale_colour_discrete(name = "Country", breaks=c("Argentina","Bolivia","Brazil", "Canada",
                                                   "Chile",
                                                   "Colombia", "Costa Rica","Dominican Republic",
                                                   "Ecuador", "El Salvador", "Guatemala",
                                                   "Honduras","Jamaica",
                                                   "Mexico","Nicaragua", "Panama","Paraguay",
                                                   "Peru","United States","Uruguay"))

### FIGURE 2: Average media trust over time by political system ##
### Categorizations based on EIU Democracy Index 2023 ###
americasbar_r2.2$democracy<-dplyr::recode(americasbar_r2.2$pais, "Argentina" = "Flawed democracy", "Bolivia" = "Hybrid regime", "Brazil" = "Flawed democracy",
                                   "Chile" = "Flawed democracy", "Colombia" = "Flawed democracy", "Ecuador" = "Hybrid regime", "Paraguay" = "Flawed democracy",
                                   "Peru" = "Hybrid regime", "Uruguay" = "Full democracy", "Canada" = "Full democracy", "United States" = "Flawed democracy",
                                   "Panama" = "Flawed democracy", "Costa Rica" = "Full democracy", "El Salvador" = "Hybrid regime", 
                                   "Guatemala" = "Hybrid regime", "Honduras" = "Hybrid regime", "Nicaragua" = "Authoritarian",
                                   "Mexico" = "Hybrid regime",
                                   "Dominican Republic" = "Flawed democracy", "Jamaica" = "Flawed democracy")

americasbar_r2.2 %>%
  group_by(democracy, pais) %>%
  summarise_at(vars(b37),
               list(name=mean), na.rm=TRUE)

demo_means<-americasbar_r2.2 %>%
  drop_na(b37) %>%
  group_by(wave, democracy) %>%
  dplyr::summarise(lci = t.test(b37, conf.level=0.95)$conf.int[1],
                   uci = t.test(b37, conf.level=0.95)$conf.int[2],
                   'fig2_sd' = sd(b37),
                   'fig2_Avg' = mean(b37)) %>%
  view

figure2<-demo_means %>%
  ggplot(aes(wave, fig2_Avg, colour=democracy)) +
  geom_point(shape = 15, size = 2) +
  geom_errorbar(aes(wave, ymin=lci, ymax=uci), width=0.4, color="grey", linewidth=.5) +
  facet_grid(~factor(democracy, c("Authoritarian","Hybrid regime", "Flawed democracy",
                                  "Full democracy"))) +
  geom_smooth() +
  geom_line(aes(group=democracy)) +
  theme_bw() +
  labs(x = "Year", y="Average trust in mass media", colour="Political System")
show(figure2)

figure2 + coord_cartesian(xlim =c(1,6), ylim = c(1.00,7.00)) + 
  theme(text = element_text(family = "serif")) +
  theme(legend.position="bottom") +  
  scale_colour_discrete(name = "Political System", limits=c("Authoritarian","Hybrid regime", "Flawed democracy",
                                                            "Full democracy")) +
  scale_linetype_discrete(guide="none")

### FIGURE 3: PERCEPTIONS OF PRESS FREEDOM ##
##creating median splits##
americasbar_r2.2<-americasbar_r2.2 %>%
  group_by(pais) %>%
  mutate(
    med_lib1 = median(lib1, na.rm=TRUE)
  ) %>%
  ungroup()

##create categorical variable for countries based on whether they were high / low in press freedom ##
americasbar_r2.2$pressfreedom_binary<-dplyr::recode(americasbar_r2.2$pais, "Argentina" = "High", "Bolivia" = "Low", "Brazil" = "High",
                                             "Chile" = "High", "Colombia" = "Low", "Ecuador" = "Low", "Paraguay" = "High",
                                             "Peru" = "High", "Uruguay" = "High", "Canada" = "High", "United States" = "High",
                                             "Panama" = "Low", "Costa Rica" = "High", "El Salvador" = "Low", 
                                             "Guatemala" = "Low", "Honduras" = "Low", "Nicaragua" = "Low",
                                             "Mexico" = "Low", "Dominican Republic" = "High", "Jamaica" = "High")

americasbar_r2.2 %>%
  group_by(pressfreedom_binary, wave) %>%
  summarise_at(vars(b37),
               list(name=mean), na.rm=TRUE)

press_freedom_means<-americasbar_r2.2 %>%
  drop_na(b37) %>%
  group_by(pressfreedom_binary, wave) %>%
  dplyr::summarise(lci = t.test(b37, conf.level=0.95)$conf.int[1],
                   uci = t.test(b37, conf.level=0.95)$conf.int[2],
                   'fig3_b37sd' = sd(b37),
                   'fig3_b37Avg' = mean(b37)) %>%
  view

figure3<-press_freedom_means %>%
  ggplot(aes(wave, fig3_b37Avg, colour=pressfreedom_binary, linetype = pressfreedom_binary)) +
  geom_point(shape = 15, size = 2) +
  geom_errorbar(aes(wave, ymin=lci, ymax=uci), width=0.4, color="grey", linewidth=.5) +
  geom_smooth() +
  geom_line(aes(group=pressfreedom_binary)) +
  scale_linetype_manual(values = c("Low" = "solid", "High" = "dashed"), guide = "none") +
  theme_bw() +
  labs(x = "Year", y="Average trust in mass media", colour="Perceptions of press freedom")
show(figure3)

figure3 + coord_cartesian(xlim =c(1,6), ylim = c(1.00,7.00)) + 
  theme(text = element_text(family = "serif")) +
  theme(legend.position="bottom") +  
  scale_colour_discrete(name = "Perceptions of press freedom", limits=c("Low", "High")) +
  scale_linetype_discrete(guide="none")

### FIGURE 4: TRUST IN ELECTIONS ###
##creating median splits##
americasbar_r2.2<-americasbar_r2.2 %>%
  group_by(pais) %>%
  mutate(
    med_b47a = median(b47a, na.rm=TRUE)
  ) %>%
  ungroup()

tabyl(americasbar_r2.2, pais, med_b47a)

##create categorical variable for trust in elections ##
americasbar_r2.2$electiontrust_binary<-dplyr::recode(americasbar_r2.2$pais, "Argentina" = "Low", "Bolivia" = "Low", "Brazil" = "Low",
                                              "Chile" = "High", "Colombia" = "Low", "Ecuador" = "Low", "Paraguay" = "Low",
                                              "Peru" = "Low", "Uruguay" = "High", "Canada" = "High", "United States" = "Low",
                                              "Panama" = "Low", "Costa Rica" = "High", "El Salvador" = "Low", 
                                              "Guatemala" = "Low", "Honduras" = "Low", "Nicaragua" = "Low",
                                              "Mexico" = "Low", "Dominican Republic" = "Low", "Jamaica" = "Low")

americasbar_r2.2 %>%
  group_by(electiontrust_binary, wave) %>%
  summarise_at(vars(b37),
               list(name=mean), na.rm=TRUE)

election_trust_means<-americasbar_r2.2 %>%
  drop_na(b37) %>%
  group_by(electiontrust_binary, wave) %>%
  dplyr::summarise(lci = t.test(b37, conf.level=0.95)$conf.int[1],
                   uci = t.test(b37, conf.level=0.95)$conf.int[2],
                   'fig5_b37sd' = sd(b37),
                   'fig5_b37Avg' = mean(b37)) %>%
  view

figure4<-election_trust_means %>%
  ggplot(aes(wave, fig5_b37Avg, colour=electiontrust_binary, linetype=electiontrust_binary)) +
  geom_point(shape = 15, size = 2) +
  geom_errorbar(aes(wave, ymin=lci, ymax=uci), width=0.4, color="grey", linewidth=.5) +
  geom_smooth() +
  geom_line(aes(group=electiontrust_binary)) +
  scale_linetype_manual(values = c("Low" = "solid", "High" = "dashed"), guide = "none") +
  theme_bw() +
  labs(x = "Year", y="Average trust in mass media", colour="Trust in Elections")
show(figure4)

figure4 + coord_cartesian(xlim =c(1,6), ylim = c(1.00,7.00)) + 
  theme(text = element_text(family = "serif")) +
  theme(legend.position="bottom") +  
  scale_colour_discrete(name = "Trust in elections", limits=c("Low", "High")) +
  scale_linetype_discrete(guide="none")

###ATTENTION TO NEWS###
##create news use variable by combining two measures (same wording exc. for one slight response option)##
americasbar_r2.2 %>%
  group_by(pais, wave) %>%
  summarise_at(vars(gi0n),
               list(name=mean), na.rm=TRUE)

americasbar_r2.2 %>%
  group_by(pais, wave) %>%
  summarise_at(vars(gi0),
               list(name=mean), na.rm=TRUE)

americasbar_r2.2$newsuse<-apply(cbind(americasbar_r2.2$gi0,americasbar_r2.2$gi0n),1,function(x) ifelse(all(is.na(x)),NA,sum(x,na.rm=T)))

##recode news use variable so that 1=never and 5=daily##
americasbar_r2.2$newsuse_r<-car::recode(americasbar_r2.2$newsuse,"1=5;2=4;3=3; 4=2; 5=1")
##M = 4.31##

###Figure 5-- standardized media trust and attention to news over time by country ###
americasbar_r2.2$newsuse_rescaled<-(americasbar_r2.2$newsuse_r - 1)/(5-1)
americasbar_r2.2$b37_rescaled<-(americasbar_r2.2$b37 - 1)/(7-1)

newsuse_means<-americasbar_r2.2 %>%
  drop_na(newsuse_r, b37) %>%
  group_by(pais, wave) %>%
  dplyr::summarise(lci1 = t.test(b37_rescaled, conf.level=0.95)$conf.int[1],
                   uci1 = t.test(b37_rescaled, conf.level=0.95)$conf.int[2],
                   lci2 = t.test(newsuse_rescaled, conf.level=0.95)$conf.int[1],
                   uci2 = t.test(newsuse_rescaled, conf.level=0.95)$conf.int[2],
                   'fig5_trustsd' = sd(b37_rescaled),
                   'fig5_trustAvg' = mean(b37_rescaled),
                   'fig5_newsusesd' = sd(newsuse_rescaled),
                   'fig5_newsuseAvg' = mean(newsuse_rescaled)) %>%
  view

figure5<-newsuse_means %>%
  ggplot(aes(wave)) +
  geom_line(aes(y = fig5_trustAvg, colour = "Media trust", linetype = "solid", group=pais), size = .5) +
  geom_line(aes(y = fig5_newsuseAvg, colour = "Attention to news", linetype = "dashed", group=pais), size = .5) +
  geom_errorbar(aes(wave, ymin=lci1, ymax=uci1), width=0.4, color="grey", linewidth=.4) +
  geom_errorbar(aes(wave, ymin=lci2, ymax=uci2), width = 0.4, color = "grey", linewidth=.4) +
  facet_wrap(~factor(pais, c("Argentina","Bolivia","Brazil", "Canada",
                             "Chile",
                             "Colombia", "Costa Rica","Dominican Republic",
                             "Ecuador", "El Salvador", "Guatemala",
                             "Honduras","Jamaica",
                             "Mexico","Nicaragua", "Panama","Paraguay",
                             "Peru","United States","Uruguay"))) +
  theme_bw() +
  labs(x = "Year", 
       y="Standardized averages",
       colour = "Measures")
show(figure5)

figure5 + coord_cartesian(xlim =c(1,5), ylim = c(0.00,1.00)) + 
  theme(text = element_text(family = "serif")) +
  theme(legend.position="bottom") +
  scale_linetype_discrete(guide="none")

### POLITICAL INTEREST ###
americasbar_r2.2 %>%
  group_by(pais, wave) %>%
  summarise_at(vars(pol1),
               list(name=mean), na.rm=TRUE)

##recode so that greater values = greater interest ##
americasbar_r2.2$pol1_r<-car::recode(americasbar_r2.2$pol1,"1=4;2=3;3=2;4=1")

americasbar_r2.2 %>%
  group_by(pais, wave) %>%
  summarise_at(vars(pol1_r),
               list(name=mean), na.rm=TRUE) %>%
  view
##Mean political interest = 2.14 on 4-point scale where 1 = none, 4 = a lot##

##FIGURE 6. Standardized trust in mass media and political interest over time by country ##
americasbar_r2.2$pol1_rescaled<-(americasbar_r2.2$pol1_r - 1)/(4-1)
##use b37_rescaled variable from earlier##

polinterestmeans2<-americasbar_r2.2 %>%
  drop_na(pol1_r, b37) %>%
  group_by(wave, democracy) %>%
  dplyr::summarise(lci1 = t.test(b37_rescaled, conf.level=0.95)$conf.int[1],
                   uci1 = t.test(b37_rescaled, conf.level=0.95)$conf.int[2],
                   lci2 = t.test(pol1_rescaled, conf.level=0.95)$conf.int[1],
                   uci2 = t.test(pol1_rescaled, conf.level=0.95)$conf.int[2],
                   'fig6_trustsd' = sd(b37_rescaled),
                   'fig6_trustAvg' = mean(b37_rescaled),
                   'fig6_polintsd' = sd(pol1_rescaled),
                   'fig6_polintAvg' = mean(pol1_rescaled)) %>%
  view

figure6<-polinterestmeans2 %>%
  ggplot(aes(wave)) +
  geom_line(aes(y = fig6_trustAvg, colour = "Media trust", linetype = "solid", group=democracy), size = .5) +
  geom_line(aes(y = fig6_polintAvg, colour = "Political interest", linetype = "dashed", group=democracy), size = .5) +
  geom_errorbar(aes(wave, ymin=lci1, ymax=uci1), width=0.4, color="grey", linewidth=.4) +
  geom_errorbar(aes(wave, ymin=lci2, ymax=uci2), width = 0.4, color = "grey", linewidth=.4) +
  facet_wrap(~democracy) +
  theme_bw() +
  labs(x = "Year", 
       y="Standardized averages",
       colour = "Measures")
show(figure6)

figure6 + coord_cartesian(xlim =c(1,6), ylim = c(0.00,1.00)) + 
  theme(text = element_text(family = "serif")) +
  theme(legend.position="bottom") +
  scale_linetype_discrete(guide="none")

###########################################
#########SUPPLEMENTARY MATERIALS###########
###########################################
library(ggpubr)
library(ggplot2)
library(sjPlot)
library(lme4)
library(nlme)
library(tidyr)
library(dplyr)

###TABLES S3a-S3c: AGE, GENDER, EDUCATION BY COUNTRY BY YEAR###
##recode age to ranges; age variable =  q2##
agesummary<-americasbar_r2.2 %>%
  group_by(wave, pais) %>%
  summarise_at(vars(q2),
               list(name=mean), na.rm=TRUE)
view(agesummary)

americasbar_r2.2$age <- car::recode(americasbar_r2.2$q2,"16:24=1;25:44=2;45:64=3;65:100=4")

agebreakdown<-americasbar_r2.2 %>%
  group_by(wave, pais, age) %>%
  tally
view(agebreakdown)

##gender = Q1; 1 = man/male, 2 = woman/female, 3 = does not identify as man or woman##
##measured using variable 'sexin' for 2023 wave##
##measured using variable 'USQ1TC' for 2023 US wave##
summary(americasbar_r2.2$q1)
tabyl(americasbar_r2.2, pais, sexin)
summary(americasbar_r2.2$usq1tc)

americasbar_r2.2$gender<-with(americasbar_r2.2, 
                              ifelse(!is.na(sexin), recode(sexin, "1=0; 2=1"),
                                     ifelse(!is.na(usq1tc), recode(usq1tc, "1=0; 2=1"),
                                            recode(q1, "1=0; 2=1"))))

gendersummary<-americasbar_r2.2 %>%
  group_by(wave, pais) %>%
  summarise_at(vars(gender),
               list(name=mean), na.rm=TRUE)
view(gendersummary)

americasbar_r2.2$q1_recode<-car::recode(americasbar_r2.2$q1,"1=0; 2=1; else=NA")

q1summary<-americasbar_r2.2 %>%
  group_by(wave, pais) %>%
  summarise_at(vars(q1_recode),
               list(name=mean), na.rm=TRUE)
view(q1summary)

americasbar_r2.2$sexin_recode<-car::recode(americasbar_r2.2$sexin,"1=0; 2=1; else=NA")

sexinsummary<-americasbar_r2.2 %>%
  group_by(wave, pais) %>%
  summarise_at(vars(sexin_recode),
               list(name=mean), na.rm=TRUE)
view(sexinsummary)

americasbar_r2.2$usq1tc_recode<-car::recode(americasbar_r2.2$usq1tc,"1=0; 2=1; else=NA")

usq1tcsummary<-americasbar_r2.2 %>%
  group_by(wave, pais) %>%
  summarise_at(vars(usq1tc_recode),
               list(name=mean), na.rm=TRUE)
view(usq1tcsummary)

##education = ed --> differs for USA (ed_usa), Canada (edcan), 2023 data (edre) ##
# Create a new 'education' variable based on recoding different sources
americasbar_r2.2$education <- with(americasbar_r2.2, 
                                   ifelse(!is.na(ed_usa), recode(ed_usa, "1:2=1; 3:4=2; 5:6=3"),
                                          ifelse(!is.na(edcan), recode(edcan, "1:4=1; 5:6=2; 7:8=3"),
                                                 ifelse(!is.na(edre), recode(edre, "0:4=1; 5=2; 6=3"),
                                                        recode(ed, "0:6=1; 7:12=2; 13:18=3")))))

educationsummary<-americasbar_r2.2 %>%
  group_by(pais, wave) %>%
  summarise_at(vars(education),
               list(name=mean), na.rm=TRUE)
view(educationsummary)
tabyl(americasbar_r2.2, edcan, wave)

### TABLE S5b. Correlations between key variables ###
### List of variables for correlation: b37 (media trust), lib1 (press freedom), 
### b47a (trust in elections), newsuse_r(attention to news), pol1 (political interest)##

### assessing linearity of relationships##
americasbar_r2.2$b37 <- as.numeric(as.character(americasbar_r2.2$b37))
americasbar_r2.2$lib1 <- as.numeric(as.character(americasbar_r2.2$lib1))

plot_data <- americasbar_r2.2 %>%
  filter(!is.na(b37) & !is.na(lib1)) %>%
  filter(b37 %in% 1:7, lib1 %in% 1:3)

trust_pressfreedom<-ggscatter(plot_data, x = "b37", y = "lib1",
                              add = "reg.line",       
                              conf.int = TRUE,
                              cor.coef = TRUE,
                              cor.method = "spearman",
                              jitter = TRUE,
                              xlab = "Media Trust", 
                              ylab = "Perceived Press Freedom")
trust_pressfreedom

vars <- americasbar_r2.2 %>% select(b37, lib1, b47a, newsuse_r, pol1)
res<-corr.test(vars, method = "spearman")
spearman_res<-corr.test(vars, method = "spearman", use = "pairwise")
spearman_res$r
describe(vars)

### FIGURE S7. Average trust in media by perceptions of press freedom and country ###

### FIGURE S8a. Average trust in media by trust in elections and country ###

election_trust_means1<-americasbar_r2.2 %>%
  drop_na(b47a, b37) %>%
  group_by(pais, wave) %>%
  dplyr::summarise(lci1 = t.test(b37, conf.level=0.95)$conf.int[1],
                   uci1 = t.test(b37, conf.level=0.95)$conf.int[2],
                   lci2 = t.test(b47a, conf.level=0.95)$conf.int[1],
                   uci2 = t.test(b47a, conf.level=0.95)$conf.int[2],
                   'fig6_b37sd' = sd(b37),
                   'fig6_b37Avg' = mean(b37),
                   'fig6_b47asd' = sd(b47a),
                   'fig6_b47aAvg' = mean(b47a)) %>%
  view

figures8a<-election_trust_means1 %>%
  ggplot(aes(wave)) +
  geom_line(aes(y = fig6_b37Avg, colour = "Media trust", linetype = "solid", group=pais), size = .5) +
  geom_line(aes(y = fig6_b47aAvg, colour = "Trust in elections", linetype = "dashed", group=pais), size = .5) +
  geom_errorbar(aes(wave, ymin=lci1, ymax=uci1), width=0.4, color="grey", linewidth=.4) +
  geom_errorbar(aes(wave, ymin=lci2, ymax=uci2), width = 0.4, color = "grey", linewidth=.4) +
  facet_wrap(~factor(pais, c("Argentina","Bolivia","Brazil", "Canada",
                             "Chile",
                             "Colombia", "Costa Rica","Dominican Republic",
                             "Ecuador", "El Salvador", "Guatemala",
                             "Honduras","Jamaica",
                             "Mexico","Nicaragua", "Panama","Paraguay",
                             "Peru","United States","Uruguay"))) +
  theme_bw() +
  labs(x = "Year", 
       y="Average trust",
       colour="Trust")
show(figures8a)

figures8a + coord_cartesian(xlim =c(1,4), ylim = c(1.00,7.00)) + 
  theme(text = element_text(family = "serif")) +
  theme(legend.position="bottom") +
  scale_linetype_discrete(guide="none")

### FIGURE S9a. Average media trust by country-level attention to news ###
news_means3<-americasbar_r2.2 %>%
  drop_na(newsuse_r) %>%
  group_by(wave, pais) %>%
  dplyr::summarise(lci = t.test(newsuse_r, conf.level=0.95)$conf.int[1],
                   uci = t.test(newsuse_r, conf.level=0.95)$conf.int[2],
                   'mean' = mean(newsuse_r),
                   .groups="drop") %>%
  view

figureS9a<-news_means3 %>%
  ggplot(aes(wave, mean, colour=pais)) +
  geom_errorbar(aes(wave, ymin=lci, ymax=uci), width=0.4, color="grey", linewidth=.5) +
  geom_point(shape = 15, size = 2) +
  geom_point(aes(wave, mean)) +
  geom_smooth() +
  geom_line(aes(y = mean, group="pais")) +
  theme_bw() +
  facet_wrap(~factor(pais, c("Argentina","Bolivia","Brazil", "Canada",
                             "Chile",
                             "Colombia", "Costa Rica","Dominican Republic",
                             "Ecuador", "El Salvador", "Guatemala",
                             "Honduras","Jamaica",
                             "Mexico","Nicaragua", "Panama","Paraguay",
                             "Peru","United States","Uruguay"))) +
  labs(x = "Year", y = "Average attention to news")
show(figureS9a)  

figureS9a<-figureS9a + coord_cartesian(xlim =c(1,5), ylim = c(1.0,5.0)) + 
  theme(text = element_text(family = "serif"))+
  theme(legend.position="none")
show(figureS9a)  

### FOR LINEAR / MULTIVARIATE MIXED EFFECTS TABLES S6-S10 ###
### centering time variable + creating a quadratic term ###
americasbar_r2.2$wave_num <- as.numeric(as.character(americasbar_r2.2$wave))
americasbar_r2.2$wave_c <- scale(americasbar_r2.2$wave_num, center = TRUE, scale = FALSE)
americasbar_r2.2$wave_c2 <- americasbar_r2.2$wave_c^2

##changing political system reference group to full democracy##
americasbar_r2.2$democracy <- relevel(factor(americasbar_r2.2$democracy), ref = "Full democracy")

### TABLE S6 ###
model1 <- lmer(b37 ~ wave_c * democracy + wave_c2 * democracy + 
                 (1 | pais), data = americasbar_r2.2)
model2 <- lmer(b37 ~ wave_c * democracy + 
                 (1 | pais), data = americasbar_r2.2)
summary(model)
confint(model)
AIC(model1)
BIC(model1)
anova(model1, model2)
tab_model(model1)

### TABLE S7 ###
americasbar_r2.2$pressfreedom_binary<-factor(americasbar_r2.2$pressfreedom_binary, levels = c("High","Low"))

model_pressfreedom<-lmer(
  b37 ~ wave_c * pressfreedom_binary + wave_c2 * pressfreedom_binary +
    (1 | pais),
  data = americasbar_r2.2)
AIC(model_pressfreedom)

model2_pressfreedom<-lmer(
  b37 ~ wave_c * pressfreedom_binary + (1 | pais),
  data = americasbar_r2.2
)
AIC(model2_pressfreedom)
anova(model_pressfreedom, model2_pressfreedom)

summary(model_pressfreedom)
confint(model_pressfreedom)
AIC(model_pressfreedom)
BIC(model_pressfreedom)
tab_model(model_pressfreedom)

### TABLE S8 ###
americasbar_r2.2$electiontrust_binary<-factor(americasbar_r2.2$electiontrust_binary, levels = c("High","Low"))

model_electiontrust<-lmer(
  b37 ~ wave_c * electiontrust_binary + wave_c2 * electiontrust_binary +
    (1 | pais),
  data = americasbar_r2.2)
AIC(model_electiontrust)

model2_electiontrust<-lmer(
  b37 ~ wave_c * electiontrust_binary + (1 | pais),
  data = americasbar_r2.2
)
AIC(model2_electiontrust)
anova(model_electiontrust, model2_electiontrust)

summary(model_electiontrust)
confint(model_electiontrust)
AIC(model_electiontrust)
BIC(model_electiontrust)
tab_model(model_electiontrust)

### TABLE S10 ###
clean_data <- na.omit(americasbar_r2.2[, c("b37_rescaled", "pol1_rescaled", "wave_c", "wave_c2", "democracy", "pais")])

long_data <- clean_data %>%
  pivot_longer(cols = c(b37_rescaled, pol1_rescaled), names_to = "outcome", values_to = "value")

model_long <- lme(
  value ~ wave_c * democracy * outcome + wave_c2 * democracy * outcome,
  random = ~1 | pais,
  data = long_data
)
summary(model_long)
tab_model(model_long)
intervals(model_long)
AIC(model_long)
BIC(model_long)